package com.zhihaoliang.animationtrain.scene

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.os.Build
import android.os.Bundle
import android.transition.Scene
import android.transition.TransitionInflater
import android.transition.TransitionManager
import android.view.View
import android.widget.RadioGroup
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.zhihaoliang.animationtrain.R
import com.zhihaoliang.animationtrain.annotation.Info
import com.zhihaoliang.animationtrain.databinding.SceneAcitvityBinding


@Info(title = "场景的转换", desc = "系统的场景转换，自定义场景转换，当低于Android 4.4, KitKat的版本通过ObjectAnimator处理")
class SceneAcitvity : AppCompatActivity(), RadioGroup.OnCheckedChangeListener {

    private lateinit var mBinding: SceneAcitvityBinding

    private lateinit var mSceneOne: Scene

    private lateinit var mSceneTwo: Scene

    private lateinit var mSceneThree: Scene

    private lateinit var mTransitionManagerFor1: TransitionManager

    private lateinit var mTransitionManagerFor2: TransitionManager

    private lateinit var mTransitionManagerFor3: TransitionManager

    private var mViewContainer: MutableList<TextView> = ArrayList()

    private lateinit var mScaleView: ViewWrapper


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mBinding = SceneAcitvityBinding.inflate(layoutInflater).apply {
            setContentView(root)
        }

        mScaleView = ViewWrapper(mBinding.screenRoot.findViewById(R.id.scene_tv_one))
        mBinding.sceneRgpSelect.setOnCheckedChangeListener(this)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) onInitScene()
        else {
            mViewContainer.add(mBinding.screenRoot.findViewById(R.id.scene_tv_one))
            mViewContainer.add(mBinding.screenRoot.findViewById(R.id.scene_tv_two))
            mViewContainer.add(mBinding.screenRoot.findViewById(R.id.scene_tv_three))
        }
    }

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    private fun onInitScene() {
        mTransitionManagerFor1 = TransitionInflater.from(this)
            .inflateTransitionManager(R.transition.scene1_transition_manager, mBinding.screenRoot)
        mTransitionManagerFor2 = TransitionInflater.from(this)
            .inflateTransitionManager(R.transition.scene2_transition_manager, mBinding.screenRoot)
        mTransitionManagerFor3 = TransitionInflater.from(this)
            .inflateTransitionManager(R.transition.scene3_transition_manager, mBinding.screenRoot)
        mSceneOne = Scene.getSceneForLayout(mBinding.screenRoot, R.layout.scene_layout_one, this)
        mSceneTwo = Scene.getSceneForLayout(mBinding.screenRoot, R.layout.scene_layout_two, this)
        mSceneThree =
            Scene.getSceneForLayout(mBinding.screenRoot, R.layout.scene_layout_three, this)
    }


    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) onSceneChange(checkedId)
        else initAnimation(checkedId)
    }

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    private fun onSceneChange(checkedId: Int) {
        when (checkedId) {
            R.id.scene_rbtn_one -> {
                //如果加载不顺畅的话使用，使用transitionTo
                //TransitionManager.go(mSceneOne)
                mTransitionManagerFor1.transitionTo(mSceneOne)
            }
            R.id.scene_rbtn_two -> mTransitionManagerFor2.transitionTo(mSceneTwo)
            R.id.scene_rbtn_three -> mTransitionManagerFor3.transitionTo(mSceneThree)
            R.id.scene_rbtn_four -> {
                TransitionManager.beginDelayedTransition(mBinding.screenRoot)
                val view = mBinding.screenRoot.findViewById<View>(R.id.scene_tv_one)
                val params =view.layoutParams
                params.width = resources.getDimensionPixelSize(R.dimen.scene_size)
                params.height = resources.getDimensionPixelSize(R.dimen.scene_size)
                view.layoutParams = params
            }
        }
    }


    private fun initAnimation(checkedId: Int) {
        when (checkedId) {
            R.id.scene_rbtn_one -> intList()
            R.id.scene_rbtn_two -> intList()
            R.id.scene_rbtn_three -> intList()
            R.id.scene_rbtn_four -> intList(true)
        }
    }

    private fun intList(isScale: Boolean = false) {
        val animSet = AnimatorSet()
        var builder: AnimatorSet.Builder? = null
        if (!isScale) {
            val moveOneX = ObjectAnimator.ofFloat(
                mViewContainer[0],
                "x",
                mViewContainer[0].x,
                mViewContainer[1].x
            )
            val moveOneY = ObjectAnimator.ofFloat(
                mViewContainer[0],
                "y",
                mViewContainer[0].y,
                mViewContainer[1].y
            )

            val moveTwoX = ObjectAnimator.ofFloat(
                mViewContainer[1],
                "x",
                mViewContainer[1].x,
                mViewContainer[2].x
            )
            val moveTwoY = ObjectAnimator.ofFloat(
                mViewContainer[1],
                "y",
                mViewContainer[1].y,
                mViewContainer[2].y
            )

            val moveThreeX = ObjectAnimator.ofFloat(
                mViewContainer[2],
                "x",
                mViewContainer[2].x,
                mViewContainer[0].x
            )
            val moveThreeY = ObjectAnimator.ofFloat(
                mViewContainer[2],
                "y",
                mViewContainer[2].y,
                mViewContainer[0].y
            )
            builder =
                animSet.play(moveOneX).with(moveOneY).with(moveTwoX).with(moveTwoY).with(moveThreeX)
                    .with(moveThreeY)
        }


        val scale = if (isScale) {
            if (mScaleView.target.scaleX.toInt() == 1) 2F else null
        } else {
            if (mScaleView.target.scaleX.toInt() == 2) 1F else null
        }
        if (scale != null) {
            val width = ObjectAnimator.ofFloat(
                mScaleView.target,
                "scaleX",
                scale
            )
            val height = ObjectAnimator.ofFloat(
                mScaleView.target,
                "scaleY",
                scale
            )
            if (builder == null) {
                animSet.play(width).with(height)
            } else {
                builder.with(width).with(height)
            }
        }

        //设置大小的变化
        /* if (size != null) {
             val width = ObjectAnimator.ofInt(
                 mScaleView,
                 "trueWidth",
                 size[0],
                 size[1]
             )
             val height = ObjectAnimator.ofInt(
                 mScaleView,
                 "trueHeight",
                 size[0],
                 size[1]
             )
             builder.with(width).with(height)
         }*/


        animSet.duration = 500
        animSet.start()
    }

    private fun initSize(size: Int): List<Int>? {
        val oldSize = mScaleView.target.width
        if (size - oldSize > 2 || oldSize - size > 2) {
            return listOf(oldSize, size)
        }
        return null
    }

    private fun initScale(size: Int): Float? {
        val oldSize = mScaleView.target.scaleX
        if (size - oldSize > 2 || oldSize - size > 2) {
            return size.toFloat() / oldSize.toFloat()
        }
        return null
    }

    private class ViewWrapper(val target: TextView) {

        fun setTrueWidth(width: Int) {
            target.layoutParams.width = width
            target.requestLayout() //必须调用，否则宽度改变但UI没有刷新
        }

        fun getTrueWidth(): Int {
            return target.layoutParams.width
        }

        fun setTrueHeight(width: Int) {
            target.layoutParams.height = width
            target.requestLayout() //必须调用，否则宽度改变但UI没有刷新
        }

        fun getTrueHeight(): Int {
            return target.layoutParams.height
        }
    }

}
